#ifndef SERIAL_H_
#define SERIAL_H_

/**
 * Handles serial communication through the UART interface
 * @date May 25, 2011
 * @author David Asabina
 * @note FREQ_MHz must be set in globals.h
 */

/**
+------------+------------------------------------------------+
| REG        | DESCRIPTION             	    	       	      |
+------------+------------------------------------------------+
| UBBRn[H:L] | USART Baud Rate Reg                            |
| UDRn       | USART Data Reg        	    	       	      |
| XCKn       | Transfer Clock pin     	    	       	      |
| TxDn       |                               	       	      |
| RxDn       |                               	       	      |
| UCSRnA     | USART Control & Status Regs     	       	      |
| UCSRnB     |                               	       	      |
| UCSRnC     |                               	       	      |
+------------+------------------------------------------------+
**/

#include <avr/io.h>
#include "globals.h"

/**
 * Parity constants
 */
#define PARITYEVEN 3
#define PARITYODD 2
#define PARITYDISABLED 0

/**
 * Mode of Operation constants
 */
#define MODESYNC 1
#define MODEASYNC 0

/**
 * Configures the UART interface
 * @param baudrate transmission speed of the module
 * @param mode the operation mode (sync or async)
 * @param parity the needed parity
 * @param stop amount of stop bits (1 or 2) 
 * @param charsize the bitlength of the transmit word
 * @code
 * serialInit(9600, MODESYNC, PARITYEVEN, 1, 8);
 * @endcode
 */
void serialInit(uint8_t baudrate, uint8_t mode, uint8_t parity, uint8_t stop, uint8_t charsize);

/**
 * Sets the mode of operation for the module
 * @param mode the mode of operation
 * @attention make use of Mode of Operation constants
 * @code
 * setUSARTModeOfOperation(MODESYNC);
 * @endcode
 */
void setUSARTModeOfOperation(uint8_t mode);

/**
 * Sets the parity of the module
 * @param mode the parity setting
 * @attention make use of Parity constants
 * @code
 * setUSARTParity(PARITYEVEN);
 * @endcode
 */
void setUSARTParity(uint8_t mode);

/**
 * Sets the number of stop bits for the module
 * @param bits the amount of stop bits
 */
void setUSARTStopBits(uint8_t bits);

/**
 * Sets the bitlength of the word to be transmitted
 * @param size the bitlength of the word
 */
void setUSARTCharSize(uint8_t size);

/**
 * Sets the baudrate for the given module
 * @param baudrate intended baudrate for serial communication
 */
void setUSARTBaudRate(uint8_t baudrate);

/**
 * Calculates the register value for a given baudrate
 * @param baudrate intented baudrate for serial communication
 * @param registerval the registerval to be set
 * @return the UBRR register value
 */
uint16_t calcUSARTBaudRate(uint8_t baudrate);

/**
 * pushes a character to the module for tranmission
 * @param char the character to be transmitted
 * @param timeout the maximum time to wait for transmission
 */
void putUSART(uint8_t bitword, uint8_t timeout);

/**
 * Sends a string with the UART module
 * @param string the string to be written to the UART module
 */
//void sendUSART(uint8_t string);

/**
 * Reads data from the UART module
 * @param maxLen maximum length of the string to be read
 */
void readUSART(uint8_t maxLen);

#endif
